<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>示例：any 函数</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f0f4f8;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }

        .container {
            background-color: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            width: 400px;
            text-align: center;
        }

        h1 {
            font-size: 24px;
            color: #333;
            margin-bottom: 20px;
        }

        input,
        button {
            width: 100%;
            padding: 10px;
            margin: 10px 0;
            border-radius: 5px;
            border: 1px solid #ddd;
            font-size: 16px;
        }

        input:focus,
        button:focus {
            outline: none;
            border-color: #007bff;
        }

        button {
            background-color: #007bff;
            color: white;
            cursor: pointer;
        }

        button:hover {
            background-color: #0056b3;
        }

        .result {
            margin-top: 20px;
            font-size: 18px;
            color: #28a745;
        }

        .error {
            color: #dc3545;
        }
    </style>
</head>

<body>

    <div class="container">
        <h1>any 函数 示例</h1>
        <p>请输入一个数组，查看是否存在满足条件的元素。</p>

        <label for="arrayInput">数组 (例如：[1, 0, 3, 4])</label>
        <input type="text" id="arrayInput" placeholder="请输入数组，使用逗号分隔" />

        <label for="conditionInput">条件 (例如：非零值为真)</label>
        <input type="text" id="conditionInput" placeholder="输入条件，比如：x > 2" />

        <button onclick="checkAny()">检查</button>

        <div class="result" id="result"></div>
    </div>

    <script>
        // `any` 函数的实现
        const any = (arr, fn = Boolean) => arr.some(fn);

        // 用来处理用户输入并执行检查的函数
        function checkAny() {
            const arrayInput = document.getElementById('arrayInput').value;
            const conditionInput = document.getElementById('conditionInput').value;
            const resultDiv = document.getElementById('result');

            try {
                // 将输入的字符串转换为数组
                const arr = arrayInput.split(',').map(item => {
                    // 处理数字数组
                    return isNaN(item) ? item.trim() : parseFloat(item.trim());
                });

                // 将输入的条件表达式转换为函数
                const conditionFn = new Function('x', `return ${conditionInput}`);

                // 使用 `any` 函数进行检查
                const result = any(arr, conditionFn);

                // 显示结果
                resultDiv.innerHTML = result ?
                    `<span style="color: #28a745;">条件成立！至少有一个元素满足条件。</span>` :
                    `<span class="error">没有任何元素满足条件。</span>`;
            } catch (e) {
                resultDiv.innerHTML = `<span class="error">输入有误，请检查数组格式和条件表达式。</span>`;
            }
        }
    </script>

</body>

</html>